package com.sugon.aop;

import com.sugon.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.SQLException;

/**
 * @author ： YuXD
 * @description： ERMS全局异常Handler
 * @date ： 2020-11-24 上午 11:23
 * @version: 1.0
 */
@Slf4j
@ControllerAdvice
public class ErmsGlobalExceptionHandler {

    /**
     * 用户认证相关的异常统一拦截
     *
     * @param e
     * @return
     */
    @ResponseBody
    @ExceptionHandler({UnknownAccountException.class, LockedAccountException.class, IncorrectCredentialsException.class, AuthenticationException.class})
    public R handleUnknownAccountException(RuntimeException e) {
        log.error(e.getMessage());
        return R.error(e.getMessage());
    }

    /**
     * customException
     * @param e
     * @return
     */
    @ExceptionHandler(SQLException.class)
    public R customException(Exception e) {
        log.error(e.getMessage());
        return R.error(e.getMessage());
    }

}
